//
//  CreateManagerViewController.m
//  图片故事
//
//  Created by 大帅逼 on 16/1/6.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "CreateManagerViewController.h"
#import "CreateStroyViewController.h"
#import "WitleViewController.h"
#import "ImageProcessViewController.h"
#import "LocationViewController.h"
#import "StyleViewController.h"

@interface CreateManagerViewController ()

@property(nonatomic,strong) UIView              *containerView;             // 视图管理
@property(nonatomic,strong) UIViewController    *createViewController;      // 编写的视图
@property(nonatomic,strong) UIViewController    *witleViewController;       // 输入的视图
@property(nonatomic,strong) UIViewController    *imageProcessViewControler; // 图片修改的视图
@property(nonatomic,strong) ImageProcessViewController *imageProcess;       // 图片视图
@property(nonatomic,strong) UIViewController    *locationViewController;    // 位置控制器
@property(nonatomic,strong) UIViewController    *styleViewController;       // 图文排版视图
@property(nonatomic,strong) WitleViewController *witleVC;

@end

@implementation CreateManagerViewController

// 视图管理的单例
+ (CreateManagerViewController *) shardManger {
    
    static CreateManagerViewController *manger = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        manger = [[CreateManagerViewController alloc] init];
    });
    
    return manger;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化页面
    [self initView];
    
    // 初始化通知
    [self initNotification];
}

/* - - - - - -- - - - - - - - -页面相关设置 - - - - - - - - - -- -  */

- (void) initView {
    
    [self.view addSubview:self.containerView];
    
    // 添加子视图
    [self addChildViewController:self.createViewController];
    
    [self addChildViewController:self.witleViewController];
    [self.witleViewController.view setAlpha:0];

    [self addChildViewController:self.imageProcessViewControler];
    [self.imageProcessViewControler.view setAlpha:0];
    
    [self addChildViewController:self.locationViewController];
    [self.locationViewController.view setAlpha:0];
    
    [self addChildViewController:self.styleViewController];
    [self.styleViewController.view setAlpha:0];
}

- (void)addChildViewController:(UIViewController *)childController {
    
    [super addChildViewController:childController];
    
    [childController didMoveToParentViewController:self];
    
    [self.containerView addSubview:childController.view];
}

/* - - - - - -- - - - - - - - -添加通知 - - - - - - - - - -- -  */

- (void) initNotification {
    

}

/* - - - - - -- - - - - - - - -点击事件相关设置 - - - - - - - - - -- -  */

- (void) showWitleView {

    [UIView animateWithDuration:0.3 animations:^{
        
        if (self.witleViewController.view.alpha > 0) {
            
            [self.witleViewController.view setAlpha:0];
        }else {

            [_witleVC.contextTextView becomeFirstResponder];
            [self.witleViewController.view setAlpha:1];
        }
    } completion:nil];
}

- (void) showLocationViewController {
    
    [UIView animateWithDuration:0.3 animations:^{
        
        if (self.locationViewController.view.alpha > 0) {
            
            [self.locationViewController.view setAlpha:0];
        }else {
            
            [self.locationViewController.view setAlpha:1];
        }
    } completion:nil];
}

- (void) showStyleViewController {
    
    [UIView animateWithDuration:0.3 animations:^{
        
        if (self.styleViewController.view.alpha > 0) {
            
            [self.styleViewController.view setAlpha:0];
        }else {
            
            [self.styleViewController.view setAlpha:1];
        }
    } completion:nil];
}

- (void) showImageProcessViewControllerWithImage : (UIImage *) image {
    
    if (image != nil) {
        _imageProcess.oldImage = image;
    }else {
    
        _imageProcess.oldImage = nil;
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        
        if (self.imageProcessViewControler.view.alpha > 0) {
            
            [self.imageProcessViewControler.view setAlpha:0];
        }else {
            
            [self.imageProcessViewControler.view setAlpha:1];
        }
    } completion:nil];
}

#pragma mark -- 懒加载

- (UIView *) containerView {
    
    if (!_containerView) {
        _containerView = [[UIView alloc] initWithFrame:self.view.bounds];
    }
    
    return _containerView;
}

- (UIViewController *)createViewController {
    
    if (!_createViewController) {
        
        CreateStroyViewController *createVC = [[CreateStroyViewController alloc] init];
        
        _createViewController = [[UINavigationController alloc] initWithRootViewController:createVC];
    }
    return _createViewController;
}

- (UIViewController *)witleViewController {
    
    if (!_witleViewController) {
        
        _witleVC = [[WitleViewController alloc] init];
        
        _witleViewController = [[UINavigationController alloc] initWithRootViewController:_witleVC];
    }
    return _witleViewController;
}

- (UIViewController *)imageProcessViewControler {

    if (!_imageProcessViewControler) {
        
        _imageProcess = [ImageProcessViewController shardImagePrecessViewController];
        
        _imageProcessViewControler = [[UINavigationController alloc] initWithRootViewController:_imageProcess];
    }
    return _imageProcessViewControler;
}

- (UIViewController *)locationViewController {
    
    if (!_locationViewController) {
        
        LocationViewController *locationVC = [[LocationViewController alloc] init];
        
        _locationViewController = [[UINavigationController alloc] initWithRootViewController:locationVC];
    }
    return _locationViewController;
}

- (UIViewController *)styleViewController {
    
    if (!_styleViewController) {
        
        StyleViewController *styleVC = [[StyleViewController alloc] init];
        
        _styleViewController = [[UINavigationController alloc] initWithRootViewController:styleVC];
    }
    return _styleViewController;
}

@end
